<?php

namespace ai\app\libs;

use Yii;

require_once(__DIR__ . "/wechat/wechat.class.php");

class Wechatapi extends \Wechat
{
    public $wechat_id;

    private $db;


    public function __construct($wx_account)
    {
        $this->db = \Ai::$db;
        $options = $this->init($wx_account);
        parent::__construct($options);
    }

    public function getJsParams()
    {
        $url = 'http://' . $_SERVER['SERVER_NAME']  . $_SERVER["REQUEST_URI"];
        $jsStr = $this->getJsSign($url);
        return $jsStr;
    }

    private function init($wx_account)
    {
        $wechatInfo = \Ai::$db->table->wx_account->query->where(['account' => $wx_account])->one();
        if (!$wechatInfo) {
            echo "no wechat account";
            exit;
        }
        $this->wechat_id = $wechatInfo['id'];
        $wechatOpctions = array(
            "token" => $wechatInfo['token'],
            //"encodingaeskey" => $wechatInfo['encoding_aes_key'],
            "appid" => $wechatInfo['appid'],
            "appsecret" => $wechatInfo['secret'],
            "debug" => false,
            //"logcallback"=>false
        );
        return $wechatOpctions;
    }

    protected function log($res)
    {

    }

    /**
     * 重载设置缓存
     * @param string $cachename
     * @param mixed $value
     * @param int $expired
     * @return boolean
     */
    protected function setCache($cachename, $value, $expired)
    {
        $expire = time() + $expired;
        if (strstr($cachename, 'wechat_access_token')) {
            \Ai::$db->table->wx_account->update(array('access_token_time' => $expire, 'access_token' => $value),"id=".$this->wechat_id)->execute();
        } else if (strstr($cachename, 'wechat_jsapi_ticket')) {
            \Ai::$db->table->wx_account->update(array('js_ticket_time' => $expire, 'js_ticket' => $value),"id=".$this->wechat_id)->execute();
        }
        return true;
    }

    /**
     * 重载获取缓存
     * @param string $cachename
     * @return mixed
     */
    protected function getCache($cachename)
    {
        $arr = \Ai::$db->table->wx_account->query->where(['id' => $this->wechat_id])->one();
        $time = time();
        if (strstr($cachename, 'wechat_access_token')) {
            if ($arr['access_token_time'] >= $time) {
                $access_token = isset($arr['access_token']) ? $arr['access_token'] : false;
                return $access_token;
            } else {
                return false;
            }
        } else if (strstr($cachename, 'wechat_jsapi_ticket')) {
            if ($arr['js_ticket_time'] >= $time) {
                $ticket = isset($arr['js_ticket']) ? $arr['js_ticket'] : false;
                return $ticket;
            } else {
                return false;
            }
        }
    }

    /**
     * 重载清除缓存
     * @param string $cachename
     * @return boolean
     */
    protected function removeCache($cachename)
    {
        if (strstr($cachename, 'wechat_access_token')) {
            \Ai::$db->table->wx_account->update(array('access_token_time' => 0, 'access_token' => ''),"id=".$this->wechat_id)->execute();
        } else if (strstr($cachename, 'wechat_jsapi_ticket')) {
            \Ai::$db->table->wx_account->update(array('js_ticket_time' => 0, 'js_ticket' => ''),"id=".$this->wechat_id)->execute();
        }
        return true;
    }
}

